<?php

include_once("core/controller.php");
include_once("core/groups.php");
include_once("core/versionHandler.php");

/**
 * editUser
 *
 * @copyright inovia
 * @package oolime_manager 
 */
class EditUser extends Controller{
  private $step;
  
  public function isLegitimate(){

    // steps
    // 0 : id is given -> display the user infos
    // 1 : id, username, mdp, mpd2, group, email are given -> change user infos
	  $this->step = $this->request->get("step");
    if(!isset($this->step) || $this->step==0){
    // STEP 0
      $this->step=0;
      $tabVerify = Array(
  	    "id"  => Array("unsigned int","Please provide a user id")
  	  );
      return $this->request->check($tabVerify);
    }
    else{
    // STEP 1
      $this->step=1;

      $tabVerify=array(
        "id"  => Array("unsigned int","Please provide a user id"),
  	    "username"  => Array("string","Please provide the username"),
  	    "group"  => Array("string","Please provide the group"),
  	    "email"  => Array("email","Please provide a valid email")
      );
      // the password is optional
      $mdpOK=true;
      if($this->request->contains(array('mdp')) && $this->request->get('mdp')!=''){
        $tabVerify["mdp"]=Array("string","Please provide a password");
        $tabVerify["mdp2"]=Array("string","Please confirm the password");
        if($this->request->get("mdp") != $this->request->get("mdp2")){
          $this->response->addError("Passwords are different","password");
          $mdpOK=false;
        }
      }

      return ($this->request->check($tabVerify) && $mdpOK);
    }
  }

  public function actions(){
    // STEP 0
    if($this->step == 0){
      include_once("core/user.php");
      $userInfos = new User("r","id",$this->request->get("id"));
      if(!$userInfos->isOK()){
        $this->response->addError("This user does not exist","id");
      }else{
        // read user infos
        $tab = Array(
          "id"  => "uid",
          "user"  => "uuser",
          "group" => "ugroup",
          "email" => "uemail"
        );
        $node = $userInfos->toXML("user",$tab);
        $this->response->root->appendChild($node);

        // add existing groups
        $groupsNode = new Groups();
        $groupsNode->getGroups();
        $this->response->setXSLT("views/manager/editusers.xsl");
      }
    }
    
    else
    // STEP 1
    if($this->step == 1){

      include_once("core/user.php");
      $userInfos = new User("rw","id",$this->request->get("id"));
      if(!$userInfos->isOK()){
        $this->response->addError("This user does not exist","id");
      }else{
        // modify the user infos
        // email
        $userInfos->field->email = $this->request->get("email");

        // check existing groups
        $groupsNode = new Groups();
        $arrayGroups = $groupsNode->getArrayGroups();
        if(in_array($this->request->get("group"), $arrayGroups)){
          $userInfos->field->group = $this->request->get("group");
        }else{
          $this->response->addError("The group ".$this->request->get("group")." does not exist : field not changed","group");
        }

        // check username
        $checkUser=new User("r","user",$this->request->get("username"));
        if($checkUser->isOk()){
          if($checkUser->field->id != $this->request->get("id"))
            $this->response->addError("The username already exists : field not changed","user");
        }else{
          $userInfos->field->user = $this->request->get("username");
        }

        // password
        if($this->request->contains(array('mdp')) && $this->request->get('mdp')!=''){
          $userInfos->field->password = md5($this->request->get("mdp"));
        }

        $userInfos = null;
        
        // go back to the user page with a new node
  	    $Node = $this->response->doc->createElement("usermodified");
  	    $this->response->root->appendChild($Node);
  	    $versionH=new VersionHandler("");
        $this->response->add($versionH->getMyVersion());
        include_once("controls/manager/homeUsers.php");
        new HomeUsers();
      }
    }
  }
  
  public function defaultActions(){
    if($this->step == 1){
      include_once("core/user.php");
      $userInfos = new User("r","id",$this->request->get("id"));
      if(!$userInfos->isOK()){
        $this->response->addError("This user does not exist","id");
      }else{
        // read user infos
        $tab = Array(
          "id"  => "uid",
          "user"  => "uuser",
          "group" => "ugroup",
          "email" => "uemail"
        );
        $node = $userInfos->toXML("user",$tab);
        $this->response->root->appendChild($node);

        // add existing groups
        $groupsNode = new Groups();
        $groupsNode->getGroups();
        $this->response->setXSLT("views/manager/editusers.xsl");
      }
    }
    else{
      $versionH=new VersionHandler("");
      $this->response->add($versionH->getMyVersion());
      include_once("controls/manager/homeUsers.php");
      new homeUsers();
    }
  }
  
  
}

?>
